{% load i18n %}
{% extends "base.html" %}
{% block blogcontainer %}
<SCRIPT TYPE="text/javascript" SRC="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js" djConfig="parseOnLoad: true"></SCRIPT>
<script type="text/javascript" src="/js/form/src.js" charset="utf-8"></script>
<script type="text/javascript">
dojo.require("dojo.io.iframe");

var template_result = "";
var template_image = "<img src='__file__' alt='' title=''>";
var template_image_comic = "[comic]<img src='__file__' alt='comic' title=''>[/comic]";
var template_flash = "<script type=\"text/javascript\">";
template_flash += "var flashvars = {};";
template_flash += "var params = {};";
template_flash += "var attributes = {};";
template_flash += "swfobject.embedSWF(\"__file__\", \"myAlternativeContent___file__\", \"700\", \"525\", \"9.0.0\", \"/js/swfobject/expressInstall.swf\", flashvars, params, attributes);";
template_flash += "</scr" + "ipt>";
template_flash += "<div id=\"myAlternativeContent___file__\">";
template_flash += "<a href=\"http://www.adobe.com/go/getflashplayer\">";
template_flash += "<img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash player\" />";
template_flash += "</a>";
template_flash += "</div>";

var url_image = "/rpc/uploadImage";
var url_flash = "/rpc/uploadFlash";

var send_config = {
	// The target URL on your webserver:
	url: "",
	
	// The HTTP method to use, form specified POST:
	method: "POST",
	
	// The form node, which contains the
	// to be transfered form elements:
	form: "file_upload",
	
	// The used data format:
	handleAs: "xml",
	
	// Callback on successful call:
	load: function(result, ioArgs){
		console.log(result.firstChild.textContent);
		finalresult = dojo.fromJson(result.firstChild.textContent);
		myEditor = dojo.byId("text_input_ui");
		file_url = finalresult.file_url;

		myEditor.value = myEditor.value + String(template_result).replace(/__file__/g, file_url);
		window.close();
	},
	
	// Callback on errors:
	error: function(result){alert(result);},
};
	  
function uploadFile(){
	//alert(dojo.byId("file_upload_type").value);
	switch(dojo.byId("file_upload_type").value){
		case "flash":
			send_config.url = url_flash;
			template_result = template_flash;
			break;
		case "image":
			send_config.url = url_image;
			template_result = template_image;
			break;
		case "comic":
			send_config.url = url_image;
			template_result = template_image_comic;
			break;
		default:
			return;
	}
	dojo.io.iframe.send(send_config);
}	
</script>		
<!--
	</head>
	<body class="tundra">		
	-->


<div id="blogpost">
     {% if error %}
    <div class="error">
        {{error}}
    </div>
     {% endif %}
    <div>
    	


    <form method=post id="previewForm" onsubmit="return parseBlogContent();" dojoType="dijit.form.Form">
        <h2>
            {% ifequal action "addBlog" %}
            {% trans "Add" %} {% ifequal entrytype "blog"%}{% trans "Blog" %}{% endifequal %}{% ifequal entrytype "page"%}{% trans "Page" %}{% endifequal %}
            {% endifequal %}
            {% ifequal action "editBlog" %}
            {% trans "Edit Entry" %}
            {% endifequal %}
            {% ifequal action "editBlogReaction" %}
            {% trans "Edit Reaction" %}
            {% endifequal %}
        </h2>
        {% ifnotequal action "editBlogReaction" %}
        <div id="blogtitle">
         <input name="title_input" id="title_input" type="text" size="60"
               value="{% if blog %}{{blog.title}}{% endif %}">
            <label for="title_input"><b>{% trans "Title" %}</b>({% trans "required" %})</label>
        </div>		
		<div id="blogtype">
        {% if entrytypeList %}
        <select id="entrytype" name="entrytype">
        {% for item in entrytypeList %}
        <option value="{{item}}" {% if blog.entrytype %}{% ifequal blog.entrytype item %}selected{% endifequal %}{%endif%}>{{ item }} </option>
        {% endfor %}
        </select>
        {% endif %}
		<label for="entrytype"><b>{% trans "Entry type" %}</b>({% trans "-" %}) </label>
		</div>
		
		<div id="statustype">
        {% if statusList %}
        <select id="status" name="status">
        {% for item in statusList %}
        <option value="{{item}}" {% if blog.status %}{% ifequal blog.status item %}selected{% endifequal %}{%endif%}>{{ item }} </option>
        {% endfor %}
        </select>
        {% endif %}
		<label for="status"><b>{% trans "Status" %}</b>({% trans "-" %}) </label>
		</div>		
		
        <div id="blogtags">
         <input type=text size="60" id="tags" name="tags" value="{% if blog %}{{blog.tags_commas}}{% endif %}">
         <label for="tags"><b>{% trans "Tags" %}</b>({% trans "comma separated without space" %}) </label>
        </div>
        <div id="blogchapter">
         <input type=text size="60" id="chapter" name="chapter" value="{% if blog %}{{blog.chapter}}{% endif %}">
         <label for="chapter"><b>{% trans "Chapter Tag" %}</b>({% trans "single word or phrase" %}) </label>
        </div>
        <div id="book">
         <input type=text size="60" id="book" name="book" value="{% if blog %}{{blog.book}}{% endif %}">
         <label for="book"><b>{% trans "Book Tag" %}</b>({% trans "single word or phrase" %}) </label>
        </div>
        <div id="date">
         <input type=text size="60" id="date" name="date" value="{% if blog %}{{blog.date}}{% endif %}">
         <label for="date"><b>{% trans "Date" %}</b>({% trans "Date-time phrase to post" %}) </label>
        </div>
		{% endifnotequal %}

        {% ifequal action "editBlog"%}
        <div id="blogpermalink">
         <input type=text size="60" id="permalink" name="permalink" value="{% if blog %}{{blog.permalink}}{% endif %}">
         <label for="permalink"><b>{% trans "Permalink" %}</b> </label>
        </div>
        {% endifequal %}

        {% ifequal action "editBlogReaction" %}
        <h2 class="comments-post">{% trans "About the author" %}</h2>
        <div>
            <input id="name_input" name="name_input" type=text size="60" value="{% if blogReaction %}{{blogReaction.user}}{% endif %}">
            <label for="name_input"><b>{% trans "Name" %}</b> ({% trans "required" %})</label>
        </div>
        <div>
            <input id="mail" name="mail" type=text size="60" value="{% if blogReaction %}{{blogReaction.authorEmail}}{% endif %}">
            <label for="mail"><b>{% trans "Mail" %}</b> ({% trans "required" %})</label>
        </div>
        <div>
            <input id="website" name="website" type=text size="60" value="{% if blogReaction %}{{blogReaction.authorWebsite}}{% endif %}">
            <label for="website"><b>{% trans "Website" %}</b></label>
        </div>
        {% endifequal %}

        <div id="blogpost">
        <h2>{% trans "Content" %}</h2>
		<!--<textarea dojoType="dijit.Editor" extraPlugins="['|', 'formatBlock']" minHeight="5em" id="text_input_ui" name="text_input_ui" rows="20" cols="75">-->
        <textarea minHeight="5em" id="text_input_ui" name="text_input_ui" rows="20" cols="75">{% if blog %}{{blog.content}}{% endif %}{% if blogReaction %}{{blogReaction.content}}{% endif %}</textarea>		
        <input type="hidden" name="text_input" id="text_input">
        </div>
        
        {% ifequal action "editBlog" %}
        {% if blog %}
        <input name="blogId" type="hidden" value="{{blog.key.id}}">
        {% endif %}
        {% endifequal %}

        {% ifequal action "editBlogReaction" %}
        {% if blogReaction %}
        <input name="blogReactionId" type="hidden" value="{{blogReaction.key.id}}">
        {% endif %}
        {% endifequal %}
        <br>
		<button dojoType="dijit.form.Button" iconClass="dijitEditorIcon dijitEditorIconSave" type=submit>
					Save
		</button>
        <br><br>
    </form>
<form dojoType="dijit.form.Form" id="file_upload" name="file_upload" enctype="multipart/form-data" method="post" >
	 <select id="file_upload_type" name="file_upload_type">
	 <option value="comic">comic</option>
	 <option value="image">image</option>
	 <option value="flash">flash</option>
	 </select>
	<input type="file" id="file" name="fileToUpload">
	<input type="button" value="upload" onclick="uploadFile();">
</form>	
</div>
</div>

<script type="text/javascript">

    function parseBlogContent(){
        //form validator:
		
		//dojo example
		//dojo.byId("text_input").value = dijit.byId("text_input_ui").getValue(false);
		
		//plain example
		dojo.byId("text_input").value = dojo.byId("text_input_ui").value;

        return true;
    }

</script>

{% endblock %}

